package jp.co.sharp.android.parents.kidsguard;

import android.app.Application;
import android.content.SyncStatusObserver;

import com.android.bluetooth.parentble.ParentBleTransfer;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import jp.co.sharp.android.parents.kidsguard.activity.bluetooth.BluetoothSearchActivity;
import jp.co.sharp.android.parents.kidsguard.entity.CacheEntity;
import jp.co.sharp.android.parents.kidsguard.entity.KidInfoEntity;
import jp.co.sharp.android.parents.kidsguard.utils.ParentBleTransferCallbackImpl;

/**
 *
 */

public class App extends Application {

    private ParentBleTransfer mParentBleTransfer;
    private CacheEntity cacheEntity;
    private List<String> mLsIsMimamori = new ArrayList<>();
    // 0:ロック画面   1:画面を開く  2:ホームボタン
    private int mAppStatus = -1;

    public CacheEntity getCacheEntity() {
        return cacheEntity;
    }

    public void setCacheEntity(CacheEntity cacheEntity) {
        this.cacheEntity = cacheEntity;
    }

    public ParentBleTransfer getmParentBleTransfer() {
        return mParentBleTransfer;
    }

    public void setmParentBleTransfer(ParentBleTransfer mParentBleTransfer) {
        this.mParentBleTransfer = mParentBleTransfer;
    }

    public List<String> getmLsIsMimamori() {
        return mLsIsMimamori;
    }

    public void addmLsIsMimamori(String pMacAddress) {
        if (null != mLsIsMimamori) {
            mLsIsMimamori.add(pMacAddress);
        }
    }

    public int getAppStatus() {
        return mAppStatus;
    }

    public void setAppStatus(int mAppStatus) {
        this.mAppStatus = mAppStatus;
    }

    @Override
    public void onCreate() {
        cacheEntity = new CacheEntity();
        //将cacheEntity里面的信息存储到本地
        List<KidInfoEntity> kidInfoEntities= getRecentSubList(getApplicationContext().getFilesDir().getPath().toString());
        if (kidInfoEntities.size() != 0){
            cacheEntity.setKidLists(kidInfoEntities);
        }
        //存储app cacheEntity里面的信息到本地
//        saveRecentSubList(getFilesDir().getPath().toString(), ((App)getApplication()).getCacheEntity().getKidLists());
//        saveRecentSubList(getFilesDir().getPath().toString(), cacheEntity.getKidLists());

        setmParentBleTransfer(new ParentBleTransfer(this,new ParentBleTransferCallbackImpl(App.this)));

        //
        mLsIsMimamori = new ArrayList<>();

        super.onCreate();
    }


    /**
     * 读取缓存文件：
     */
    public static List<KidInfoEntity> getRecentSubList(String url) {
        List<KidInfoEntity> resultList = new ArrayList<>();
        String fileName =url+ "/appSave";
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
            if (ois == null){
                return null;
            }
            ArrayList<KidInfoEntity> list_ext = (ArrayList<KidInfoEntity>) ois.readObject();

            for (KidInfoEntity obj : list_ext) {
                KidInfoEntity bean = obj;
                if (bean != null) {
                    resultList.add(bean);
                }
            }
            ois.close();
        } catch (Exception e) {
            return resultList;
        }
        return resultList;
    }

    /**
     * 存储缓存文件：
     */
    public static void saveRecentSubList(String url,List<KidInfoEntity> list) {
        String fileName =url+"/appSave";
        File file = new File(fileName);
        ObjectOutputStream oos = null;
        try {
            if (!file.exists())
                file.createNewFile();
            oos= new ObjectOutputStream(new FileOutputStream(fileName));
            oos.writeObject(list);
            oos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     *  デバイスの数
     * @return
     */
    public int getMaxKidInfo() {
        if (null != cacheEntity.getKidLists()) {
            return cacheEntity.getKidLists().size();
        } else {
            return 0;
        }
    }

    /**
     * デバイスの追加
     * @param kidInfoEntity
     */
    public void insertKidInfo(KidInfoEntity kidInfoEntity) {
        if (null != cacheEntity.getKidLists()) {
            cacheEntity.getKidLists().add(kidInfoEntity);
        } else {
            List<KidInfoEntity> lsKidInfoEntity = new ArrayList<>();
            lsKidInfoEntity.add(kidInfoEntity);
            cacheEntity.setKidLists(lsKidInfoEntity);
        }

        saveRecentSubList(getFilesDir().getPath().toString(), cacheEntity.getKidLists());
    }

    /**
     * デバイスの更新
     * @param kidInfoEntity
     */
    public void updateKidInfo(KidInfoEntity kidInfoEntity) {
        if (null != cacheEntity.getKidLists()) {
            for (KidInfoEntity item : cacheEntity.getKidLists()) {
                if (item.getMacId().equals(kidInfoEntity.getMacId())) {
                    item = (KidInfoEntity)kidInfoEntity.clone();
                    break;
                }
            }
        }

        saveRecentSubList(getFilesDir().getPath().toString(), cacheEntity.getKidLists());
    }

    /**
     * デバイスの削除
     * @param kidInfoEntity
     */
    public void deleteKidInfo(KidInfoEntity kidInfoEntity) {
        if (null != cacheEntity.getKidLists()) {
            for (KidInfoEntity item : cacheEntity.getKidLists()) {
                if (item.getMacId().equals(kidInfoEntity.getMacId())) {
                    cacheEntity.getKidLists().remove(item);
                    break;
                }
            }
        }

        saveRecentSubList(getFilesDir().getPath().toString(), cacheEntity.getKidLists());
    }

    public KidInfoEntity findKidInfoBySelect(){
        if (null != cacheEntity.getKidLists()) {
            for (KidInfoEntity item: cacheEntity.getKidLists()) {
                if (item.getSelected()){
                    return item;
                }
            }
        }else {
            return null;
        }
        return null;
    }

    /**
     * デバイスのポジション
     * @param kidInfoEntity
     * @return
     */
    public int findKidInfoPosition(KidInfoEntity kidInfoEntity) {
        if (null != cacheEntity.getKidLists()) {
            for (int i = 0; i < cacheEntity.getKidLists().size(); i++) {
                if (cacheEntity.getKidLists().get(i).getMacId().equals(kidInfoEntity.getMacId())) {
                    return i;
                }
            }
        } else {
            return -1;
        }
        return -1;
    }

    /**
     * デバイスのポジション
     * @param positionIndex
     * @return
     */
    public KidInfoEntity findKidInfoByPosition(int positionIndex) {
        if (null != cacheEntity.getKidLists()) {
            return cacheEntity.getKidLists().get(positionIndex);
        }
        return null;
    }

    /**
     *
     * @param pMacAddress
     * @return ture: Mimamori  False: Not Mimamori
     */
    public boolean isMimamoriCheck(String pMacAddress) {
        if (null != cacheEntity.getKidLists()) {
            for (KidInfoEntity item: cacheEntity.getKidLists()) {
                if (item.getControlled() && item.getMacAddress().equals(pMacAddress)){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     *
     * @param pMacId
     * @return ture: Mimamori  False: Not Mimamori
     */
    public boolean isMimamoriCheckByMacId(String pMacId) {
        if (null != cacheEntity.getKidLists()) {
            for (KidInfoEntity item: cacheEntity.getKidLists()) {
                if (item.getControlled() && item.getMacId().equals(pMacId)){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     *
     * @param pMacAddress
     * @return
     */
    public KidInfoEntity findKidInfoByMacAddress(String pMacAddress) {
        if (null != cacheEntity.getKidLists()) {
            for (KidInfoEntity item: cacheEntity.getKidLists()) {
                if (item.getMacAddress().equals(pMacAddress)){
                    return item;
                }
            }
        }
        return null;
    }

    /**
     *
     * @param pMacId
     * @return
     */
    public KidInfoEntity findKidInfoByMacId(String pMacId) {
        if (null != cacheEntity.getKidLists()) {
            for (KidInfoEntity item: cacheEntity.getKidLists()) {
                if (item.getMacId().equals(pMacId)){
                    return item;
                }
            }
        }
        return null;
    }
}
